/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package control;

import cs4280.action.CinemaControl;
import cs4280.dao.MovieDAO;
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import cs4280.bean.*;
import javax.servlet.http.HttpSession;

/**
 *
 * @author PangPei
 */
public class movieControll extends HttpServlet {

    public static enum RESULT {

        OK, DUPLICATE, FAIL
    };

    /** 
     * Processes requests for both HTTP <code>GET</code> and <code>POST</code> methods.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        response.setContentType("text/html;charset=UTF-8");
        PrintWriter out = response.getWriter();
        try {
            /* TODO output your page here
            out.println("<html>");
            out.println("<head>");
            out.println("<title>Servlet movieControll</title>");  
            out.println("</head>");
            out.println("<body>");
            out.println("<h1>Servlet movieControll at " + request.getContextPath () + "</h1>");
            out.println("</body>");
            out.println("</html>");
             */
        } finally {
            out.close();
        }
    }

    @Override
    public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

        this.doPost(request, response);
    }

    @Override
    public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String jspPage = new String("");

        if ("doaddmovie".equalsIgnoreCase(request.getParameter("todo")) != false) {
            jspPage = this.doAddMoviePage(request, response);
        } else {
            jspPage =this.doIndexPage(request, response);
        }
        this.dispatchJsp(jspPage, request, response);
    }

    private void dispatchJsp(String jspPage, HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        request.getRequestDispatcher(jspPage).forward(request, response);
    }

    private String doIndexPage(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

        MovieDAO movieDAO = new MovieDAO();
        HttpSession session = request.getSession();
        if (session.getAttribute("member") != null) {
            request.setAttribute("member", session.getAttribute("member"));
        }

        request.setAttribute("movies", movieDAO.getMovies());
        return "/WEB-INF/jsp/movie.jsp";
    }

    public String doAddMoviePage(HttpServletRequest request, HttpServletResponse response) {
        Movie movie = new Movie();

        MovieDAO movieDAO = new MovieDAO();

        movie.setName(request.getParameter("name"));
        movie.setDuration(Integer.parseInt(request.getParameter("duration")));
        movie.setGenere(request.getParameter("genere"));
        movie.setActor(request.getParameter("actor"));
        movie.setMovieLang(request.getParameter("movielang"));
        movie.setOpendate(request.getParameter("opendate"));
        movie.setStoryLine(request.getParameter("storyline"));
        movie.setImgurl(request.getParameter("imgurl"));

        

        Screening screen = new Screening();
        screen.setHouseid(Integer.parseInt(request.getParameter("houseid")));
        screen.setShowtime(request.getParameter("showtime"));
        screen.setPrice(Integer.parseInt(request.getParameter("price")));


        request.setAttribute("movie", movie);
        request.setAttribute("screening", screen);
        

        switch (movieDAO.addMovie(movie,screen)) {

            case OK:
                request.setAttribute("msg", "Add movie successfully!");
                break;
            case DUPLICATE:
                request.setAttribute("msg", "Movie already exists!");
                break;
            case FAIL:
                request.setAttribute("msg", "Fail to add movie");
        }

        return "/WEB-INF/jsp/domovieAdder.jsp";
    }

    public String doPrepareAddMoviePage(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

        return "/WEB-INF/jsp/movieAdder.jsp";

    }

    public String doPrepareUpdateMoviePage(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

        Movie movie = new Movie();
        MovieDAO movieDAO = new MovieDAO();
        movie = movieDAO.getMovie(request.getParameter("moviename"));
        request.setAttribute("movie", movie);


        return "/WEB-INF/jsp/movieUpdater.jsp";

    }


    public String doUpdateMoviePage(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        Movie movie = new Movie();
        movie.setName(request.getParameter("name"));
        movie.setDuration(Integer.parseInt(request.getParameter("duration")));
        movie.setGenere(request.getParameter("genere"));
        movie.setActor(request.getParameter("actor"));
        movie.setMovieLang(request.getParameter("movielang"));
        movie.setOpendate(request.getParameter("opendate"));
        movie.setGenere(request.getParameter("genere"));
        movie.setStoryLine(request.getParameter("storyline"));
        movie.setImgurl(request.getParameter("imgurl"));

        request.setAttribute("movie", movie);
        MovieDAO movieDAO = new MovieDAO();

        switch (movieDAO.updateMovie(movie)) {

            case OK:
                request.setAttribute("msg", "Update movie successfully!");
                break;

            case FAIL:
                request.setAttribute("msg", "Fail to update movie");
        }
        return "/WEB-INF/jsp/domovieUpdater.jsp";

    }

    public String doPrepareDeleteMoviePage(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

        Movie movie = new Movie();
        MovieDAO movieDAO = new MovieDAO();
        movie = movieDAO.getMovie(request.getParameter("moviename"));
        request.setAttribute("movie", movie);


        return "/WEB-INF/jsp/movieDeleter.jsp";

    }


    public String doDeleteMoviePage(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        Movie movie = new Movie();
        MovieDAO movieDAO = new MovieDAO();
        
        
        movie = movieDAO.getMovie(request.getParameter("moviename"));
        request.setAttribute("movie", movie);

        switch (movieDAO.deleteMovie(movie)) {

            case OK:
                request.setAttribute("msg", "Delete movie successfully!");
                break;

            case FAIL:
                request.setAttribute("msg", "Fail to delete movie");
        }
        return "/WEB-INF/jsp/domovieDeleter.jsp";

    }


}
